<template>
  <div class="profile-container">
    <!-- 顶部用户信息卡片 -->
    <div class="user-header">
      <div class="user-avatar">
        <el-avatar :size="80" :src="user.avatar || '/default-avatar.png'">
          {{ user.username?.charAt(0)?.toUpperCase() }}
        </el-avatar>
      </div>
      <div class="user-basic-info">
        <h2>{{ user.username }}</h2>
        <p class="user-role">普通用户</p>
      </div>
    </div>

    <!-- 功能卡片区域 -->
    <div class="function-cards">
      <el-card class="function-card" @click="editMode = true">
        <div class="card-content">
          <el-icon class="card-icon"><User /></el-icon>
          <div class="card-info">
            <h3>个人信息</h3>
            <p>修改个人资料</p>
          </div>
          <el-icon class="arrow-icon"><ArrowRight /></el-icon>
        </div>
      </el-card>

      <el-card class="function-card">
        <div class="card-content">
          <el-icon class="card-icon"><Document /></el-icon>
          <div class="card-info" @click="goToHistory">
            <h3>健康档案</h3>
            <p>查看健康记录</p>
          </div>
          <el-icon class="arrow-icon"><ArrowRight /></el-icon>

        </div>
      </el-card>

      <el-card class="function-card">
        <div class="card-content">
          <el-icon class="card-icon"><Setting /></el-icon>
          <div class="card-info">
            <h3>系统设置</h3>
            <p>偏好设置</p>
          </div>
          <el-icon class="arrow-icon"><ArrowRight /></el-icon>
        </div>
      </el-card>
    </div>

    <!-- 健康数据汇总 -->
    <el-card class="health-summary-card">
      <template #header>
        <div class="card-header">
          <span class="header-title">健康数据汇总</span>
          <el-icon class="header-icon"><DataLine /></el-icon>
        </div>
      </template>
      <div v-if="summaryList.length > 0" class="summary-list">
        <div v-for="item in summaryList" :key="item.type" class="summary-item">
          <div class="summary-label">{{ item.label }}</div>
          <div class="summary-value">{{ item.value }}</div>
        </div>
      </div>
      <div v-else class="no-data">
        <el-empty description="暂无健康数据" />
      </div>
    </el-card>

    <!-- 退出登录按钮 -->
    <div class="logout-section">
      <el-button type="danger" class="logout-button" @click="showLogoutDialog">
        退出登录
      </el-button>
    </div>

    <!-- 个人信息编辑对话框 -->
    <el-dialog
      v-model="editMode"
      title="编辑个人信息"
      width="400px"
      :close-on-click-modal="false"
    >
      <el-form :model="user" label-width="80px" class="edit-form">
        <el-form-item label="用户名">
          <el-input v-model="user.username" disabled />
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model="user.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="user.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="user.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editMode = false">取消</el-button>
          <el-button type="primary" @click="saveUser">保存</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 退出登录确认对话框 -->
    <el-dialog
      v-model="logoutDialogVisible"
      title="退出登录"
      width="300px"
      :show-close="false"
      center
    >
      <span>确定要退出登录吗？</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="logoutDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleLogout">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'
import { ElMessage } from 'element-plus'
import { getUserInfoApi, updateUserApi, getHealthSummaryApi } from '../api/user'
import { User, Document, Setting, ArrowRight, DataLine } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const user = ref({
  username: '',
  realName: '',
  phone: '',
  email: '',
  createTime: '',
  avatar: ''
})
const editMode = ref(false)
const summaryList = ref([])
const logoutDialogVisible = ref(false)

const loadUser = async () => {
  try {
    const response = await getUserInfoApi()
    if (response) {
      user.value = response.data
    }
  } catch (error) {
    ElMessage.error('获取用户信息失败')
    console.error('获取用户信息失败:', error)
  }
}

const loadSummary = async () => {
  try {
    const response = await getHealthSummaryApi()
    if (response) {
      summaryList.value = response
    }
  } catch (error) {
    ElMessage.error('获取健康数据失败')
    console.error('获取健康数据失败:', error)
  }
}

const saveUser = async () => {
  try {
  await updateUserApi(user.value)
  editMode.value = false
    ElMessage.success('保存成功')
  } catch (error) {
    ElMessage.error('保存失败')
    console.error('保存失败:', error)
  }
}

const showLogoutDialog = () => {
  logoutDialogVisible.value = true
}

const handleLogout = () => {
  userStore.clearToken()
  ElMessage.success('退出登录成功')
  router.push('/login')
  logoutDialogVisible.value = false
}

onMounted(() => {
  loadUser()
  loadSummary()
})
const goToHistory = () => {
  router.push('/healthcheck/history')
}
</script>

<style scoped>
.profile-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f5f7fa;
}

.user-header {
  background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
  border-radius: 16px;
  padding: 30px;
  color: white;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.user-avatar {
  margin-right: 20px;
}

.user-basic-info h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.user-role {
  margin: 5px 0 0;
  opacity: 0.8;
  font-size: 14px;
}

.function-cards {
  display: grid;
  gap: 15px;
  margin-bottom: 20px;
}

.function-card {
  cursor: pointer;
  transition: all 0.3s ease;
}

.function-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.card-icon {
  font-size: 24px;
  color: #4CAF50;
  margin-right: 15px;
}

.card-info {
  flex: 1;
}

.card-info h3 {
  margin: 0;
  font-size: 16px;
  color: #303133;
}

.card-info p {
  margin: 5px 0 0;
  font-size: 12px;
  color: #909399;
}

.arrow-icon {
  color: #909399;
}

.health-summary-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-icon {
  color: #4CAF50;
  font-size: 20px;
}

.summary-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  padding: 10px;
}

.summary-item {
  background: white;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.summary-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.summary-value {
  font-size: 20px;
  font-weight: bold;
  color: #4CAF50;
}

.logout-section {
  padding: 20px 0;
  text-align: center;
}

.logout-button {
  width: 200px;
  height: 40px;
  border-radius: 20px;
}

.edit-form {
  padding: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 20px;
}

:deep(.el-card) {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

:deep(.el-dialog) {
  border-radius: 16px;
}

:deep(.el-button) {
  border-radius: 8px;
}
</style>
